package demo;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.openhtmltopdf.java2d.api.DefaultPageProcessor;
import com.openhtmltopdf.java2d.api.FSPageOutputStreamSupplier;
import com.openhtmltopdf.java2d.api.Java2DRendererBuilder;


/**
 * 代码摘抄自: <https://github.com/danfickle/openhtmltopdf/issues/73#issuecomment-291070264>
 * 
 * 原计划是利用此代码将一个页面转化成图片的,
 * 但是看了输出结果后,发现图片太模糊了!
 * 
 * 引入SVG也是一样的, 引入SVG要加载很多关联的jar包
 * 
 * @author Wxg
 *
 */
public class ImageOutput {

	public static void main(String[] args) throws Exception {
		DefaultPageProcessor processor = new DefaultPageProcessor(new FSPageOutputStreamSupplier() {
			@Override
			public OutputStream supply(int zeroBasedPageNumber) throws IOException {
				return new FileOutputStream("D:\\wxg\\openhtmltopdf\\imgs\\%page-number%.png"
						.replace("%page-number%", String.valueOf(zeroBasedPageNumber)));
			}
		}, BufferedImage.TYPE_3BYTE_BGR, "png");

		Graphics2D g2d = processor.createLayoutGraphics();

		Java2DRendererBuilder builder = new Java2DRendererBuilder();
		// builder.withUri("file:///Users/you/Documents/pdftests/showcase.htm");
		builder.withUri("http://192.168.3.5:8000/_my/106weasyprint.html");
		
		builder.useLayoutGraphics(g2d);
		
		// builder.useSVGDrawer(new BatikSVGDrawer()); // Optional
		
		builder.toPageProcessor(processor);
		builder.runPaged();

		g2d.dispose();
	}
}
